/*****************************************************************************
 * hal_efm32lg_flash.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "hal/source/hal_efm32lg/hal_efm32lg.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "hal_flash"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define EFM32LG_FLASH_PAGE_SIZE FLASH_PAGE_SIZE
#define EFM32LG_FLASH_PAGE_ID(addr) \
    ((addr - FLASH_BASE) / EFM32LG_FLASH_PAGE_SIZE)
#define EFM32LG_FLASH_PAGE_BASE(addr) \
    FLASH_BASE + EFM32LG_FLASH_PAGE_ID(addr) * EFM32LG_FLASH_PAGE_SIZE

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_flash_read(plum_u32 id, plum_u32 offset, plum_u8 *rData,
                             plum_u32 size)
{
    plum_u32 cnt = 0;
    while (cnt < size) {
        *rData++ = *(uint8_t *)offset++;
        cnt++;
    }

    return (cnt);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_write(plum_u32 id, plum_u32 offset, plum_u8 *wData,
                              plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    plum_u32 loop;
    for (loop = 0; loop < size;) {
        if (MSC_WriteWord((uint32_t *)offset, wData + loop, 4) != mscReturnOk) {
            rc = PLUM_ECODE_EIO;
            break;
        }
        loop += 4;
        rc += 4;
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_erase(plum_u32 id, plum_u32 offset, plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    plum_u8  loop;
    uint32_t addr;
    for (loop = 0; loop < offset + size;) {
        addr = EFM32LG_FLASH_PAGE_BASE(offset);
        rc   = MSC_ErasePage((uint32_t *)addr);
        if (rc) {
            rc = PLUM_ECODE_EIO;
            break;
        }
        loop += EFM32LG_FLASH_PAGE_SIZE;
        rc += EFM32LG_FLASH_PAGE_SIZE;
    }

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
